import wiringpi as gpio
from random import randint

r_pin = 11
g_pin = 12
b_pin = 13

gpio.wiringPiSetupPhys()
gpio.pinMode(r_pin, gpio.OUTPUT)
gpio.pinMode(g_pin, gpio.OUTPUT)
gpio.pinMode(b_pin, gpio.OUTPUT)

def init():
    gpio.softPwmCreate(r_pin, 0, 100)
    gpio.softPwmCreate(g_pin, 0, 100)
    gpio.softPwmCreate(b_pin, 0, 100)


def color_convert(v):
    return int(v/255*100)

def set_color(c):
    r, g, b = 0, 0, 0
    if type(c) == str:
        c = c.lower()
        c = '0x' + c if not c.startswith('0x') else c
        c = int(c, 16)
        r = (c & 0xff0000) >> 16
        g = (c & 0x00ff00) >> 8
        b = c & 0x0000ff
    elif type(c) == tuple or type(c) == list:
        r, g, b = c
    r = color_convert(r)
    g = color_convert(g)
    b = color_convert(b)
    print(r, g, b)
    gpio.softPwmWrite(r_pin, r)
    gpio.softPwmWrite(g_pin, g)
    gpio.softPwmWrite(b_pin, b)




init()

try:
    r = 0
    set_color('0x33539c')
    gpio.delay(2000)
    set_color('0xeeeeee')
    gpio.delay(2000)
    while(True):
        r, g, b = 75, 0, 130
        set_color((r, g, b))
        gpio.delay(50)
except KeyboardInterrupt:
    print('exit')

gpio.softPwmStop(11)
gpio.softPwmStop(12)
gpio.softPwmStop(13)
